#include "systemlogtable.h"
#include "systemlog.h"
#include "freyacommon.h"

SystemLogTable::SystemLogTable(const QString &connectionName) :
    AbstractTable(connectionName)
{
}

bool SystemLogTable::AppendSystemLog(const QString &connectName, const QVariant &args, QVariant &retVariant)
{
    SystemLogTable *pThis = GetInstance(connectName);

    bool bRet = false;
    SystemLog log;
    log.FromVariant(args);
    qInfo() << "SystemLogTable::AppendSystemLog:" << log.GUID;

    QMap<QString, QString> fieldMap;
    bRet = pThis->FillField("t_systemlog", args, fieldMap);

    QString sqlStri = "INSERT INTO `t_systemlog` (%1) VALUES (%2);";
    pThis->TransactionExec(sqlStri.arg(fieldMap.keys().join(",")).arg(fieldMap.values().join(",")), bRet);
    retVariant = bRet;

    return bRet;
}

bool SystemLogTable::DeleteSystemLog(const QString &connectName, const QVariant &args, QVariant &retVariant)
{
    SystemLogTable *pThis = GetInstance(connectName);

    bool bRet = false;
    qInfo() << "SystemLogTable::DeleteSystemLog:" << args;

    QStringList guidList;

    switch (args.type())
    {
    case QVariant::String:
    {
        guidList.append(args.toString());
    }
        break;
    case QVariant::StringList:
    {
        guidList.append(args.toStringList());
    }
        break;
    default:
        break;
    }

    foreach (const QString &guid, guidList)
    {
        QString sqlStr = "DELETE FROM `t_systemlog` WHERE `GUID` = '%1';";
        pThis->TransactionExec(sqlStr.arg(guid), bRet);
        retVariant = bRet;
    }

    return bRet;
}

bool SystemLogTable::QuerySystemLogByDate(const QString &connectName, const QVariant &args, QVariant &retVariant)
{
    qInfo() << "SystemLogTable::QuerySystemLogByDate:" << args;
    // 准备参数
    QDate minDate;
    QDate maxDate;
    if(QVariant::StringList == args.type())
    {
        if(args.toStringList().size() > 0)
        {
            minDate = FreyaCommon::StringToDateTime(args.toStringList().at(0)).date();
        }

        if(args.toStringList().size() > 1)
        {
            maxDate = FreyaCommon::StringToDateTime(args.toStringList().at(1)).addDays(1).date();
        }
        else
        {
            maxDate = FreyaCommon::StringToDateTime(args.toStringList().at(0)).addDays(1).date();
        }

    }
    else if(QVariant::String == args.type())
    {
        minDate = FreyaCommon::StringToDateTime(args.toString()).date();
        maxDate = FreyaCommon::StringToDateTime(args.toString()).addDays(1).date();
    }
    else
    {
        return false;
    }

    QString sqlStr = QString("SELECT * FROM `t_systemlog` WHERE `Date` BETWEEN '%1' AND '%2' ORDER BY `Date`;")
            .arg(FreyaCommon::DateTimeToString(QDateTime(minDate)))
            .arg(FreyaCommon::DateTimeToString(QDateTime(maxDate)));

    // 开始查找
    SystemLogTable *pThis = GetInstance(connectName);

    bool bRet = false;

    QVariantList recordList;

    QSqlQuery && query = pThis->TransactionExec(sqlStr, bRet);
    while(query.next())
    {
        QSqlRecord record = query.record();
        QVariantMap varMap;
        for(int i = 0; i < record.count(); ++i)
        {
            QSqlField field = record.field(i);
            varMap.insert(field.name(), field.value());
        }
        recordList.append(varMap);
    }
    retVariant = recordList;

    if(!bRet)
    {
        qWarning() << "SystemLogTable query record failed!";
        retVariant = QString(QObject::tr("SystemLogTable table query record failed!"));
    }
    return bRet;
}
